package com.neu.flashmall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.common.utils.ChineseUtils;
import com.neu.common.utils.Result;
import com.neu.flashmall.product.entity.PmsBrand;
import com.neu.flashmall.product.feign.ThirdPartyFeignService;
import com.neu.flashmall.product.mapper.PmsBrandMapper;
import com.neu.flashmall.product.service.PmsBrandService;
import com.neu.flashmall.product.vo.PmsBrandVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.Collator;
import java.util.List;

@Service
public class PmsBrandServiceImpl implements PmsBrandService {
    @Resource
    private PmsBrandMapper pmsBrandMapper;

    @Resource
    private ThirdPartyFeignService thirdPartyFeignService;

    @Override
    public PageInfo<PmsBrand> getPaged(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmsBrand> list = pmsBrandMapper.selectAll();
        PmsBrandVO vo1 = new PmsBrandVO();
        PmsBrandVO vo2 = new PmsBrandVO();
        list.sort((o1,o2)->{
            if(String.valueOf(o1.getName().charAt(0)).matches("[\u4e00-\u9fa5]")){
                //代表是中文名称
                vo1.setPmsBrand(o1);
                vo1.setEnglishName(ChineseUtils.getAllName(o1.getName()));
            }else{
                vo1.setEnglishName(o1.getName().toLowerCase());
            }
            if(String.valueOf(o2.getName().charAt(0)).matches("[\u4e00-\u9fa5]")){
                vo2.setPmsBrand(o2);
                vo2.setEnglishName(ChineseUtils.getAllName(o2.getName()));
            }else{
                vo2.setEnglishName(o2.getName().toLowerCase());
            }
            return Collator.getInstance().compare(vo1.getEnglishName(),vo2.getEnglishName());
        });
        PageInfo<PmsBrand> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<PmsBrand> selectAll() {
        return pmsBrandMapper.selectAll();
    }

    @Override
    public boolean checkBrandName(String name) {
        PmsBrand pmsBrand = pmsBrandMapper.selectByName(name);
        if(pmsBrand != null){
            return true;
        }
        return false;
    }

    @Override
    public int updateLogo(PmsBrand pmsBrand) {
        return this.pmsBrandMapper.updateByPrimaryKeySelective(pmsBrand);
    }

    @Override
    public Long insert(PmsBrand pmsBrand) {
        String first_letter = ChineseUtils.getFirstLetter(pmsBrand.getName());
        pmsBrand.setFirstLetter(first_letter);
        pmsBrand.setShowStatus(0);
        pmsBrand.setSort(0);
        pmsBrand.setHot(0);
        this.pmsBrandMapper.insertSelective(pmsBrand);
        return pmsBrand.getId();
    }

    @Override
    public int update(PmsBrand pmsBrand) {
        String first_letter = ChineseUtils.getFirstLetter(pmsBrand.getName());
        if(!first_letter.equals(pmsBrand.getFirstLetter())){
            pmsBrand.setFirstLetter(first_letter);
        }
        return pmsBrandMapper.updateByPrimaryKey(pmsBrand);
    }

    @Override
    public int delete(long id) {
        Result result = thirdPartyFeignService.deleteBrandIcon(String.valueOf(id));
        if(result.getCode().equals(2000)){
            return pmsBrandMapper.deleteByPrimaryKey(id);
        }
        return -1;
    }

    @Override
    public String setHot(Long id,int hot) {
        int result = this.pmsBrandMapper.setHot(id,hot);
        if(result > 0) return "success";
        return "数据库异常";
    }

    @Override
    public List<PmsBrand> getHot() {
        return this.pmsBrandMapper.selectHot();
    }

    @Override
    public PmsBrand getOneById(Long id) {

        return pmsBrandMapper.selectByPrimaryKey(id);
    }


}
